﻿
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RBS.DBUtility;//Please add references
namespace RBS.Core.DAL
{
	/// <summary>
	/// 数据访问类:SYS_Privilege
	/// </summary>
	public partial class SYS_Privilege
	{
		public SYS_Privilege()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string PrivilegeCode)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from SYS_Privilege");
			strSql.Append(" where PrivilegeCode=@PrivilegeCode ");
			SqlParameter[] parameters = {
					new SqlParameter("@PrivilegeCode", SqlDbType.VarChar,40)			};
			parameters[0].Value = PrivilegeCode;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(RBS.Core.Model.SYS_Privilege model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into SYS_Privilege(");
			strSql.Append("PrivilegeCode,PrivilegeName,PrivilegeType,ParentCode,Url,Sequence,LastUpdateUserName,LastUpdateUserFullName,LastUpdateTime,CreateUserName,CreateUserFullName,CreateDateTime)");
			strSql.Append(" values (");
			strSql.Append("@PrivilegeCode,@PrivilegeName,@PrivilegeType,@ParentCode,@Url,@Sequence,@LastUpdateUserName,@LastUpdateUserFullName,@LastUpdateTime,@CreateUserName,@CreateUserFullName,@CreateDateTime)");
			SqlParameter[] parameters = {
					new SqlParameter("@PrivilegeCode", SqlDbType.VarChar,40),
					new SqlParameter("@PrivilegeName", SqlDbType.NVarChar,100),
					new SqlParameter("@PrivilegeType", SqlDbType.TinyInt,1),
					new SqlParameter("@ParentCode", SqlDbType.VarChar,50),
					new SqlParameter("@Url", SqlDbType.VarChar,2000),
					new SqlParameter("@Sequence", SqlDbType.Int,4),
					new SqlParameter("@LastUpdateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateTime", SqlDbType.DateTime),
					new SqlParameter("@CreateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateDateTime", SqlDbType.DateTime)};
			parameters[0].Value = model.PrivilegeCode;
			parameters[1].Value = model.PrivilegeName;
			parameters[2].Value = model.PrivilegeType;
			parameters[3].Value = model.ParentCode;
			parameters[4].Value = model.Url;
			parameters[5].Value = model.Sequence;
			parameters[6].Value = model.LastUpdateUserName;
			parameters[7].Value = model.LastUpdateUserFullName;
			parameters[8].Value = model.LastUpdateTime;
			parameters[9].Value = model.CreateUserName;
			parameters[10].Value = model.CreateUserFullName;
			parameters[11].Value = model.CreateDateTime;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(RBS.Core.Model.SYS_Privilege model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SYS_Privilege set ");
			strSql.Append("PrivilegeName=@PrivilegeName,");
			strSql.Append("PrivilegeType=@PrivilegeType,");
			strSql.Append("ParentCode=@ParentCode,");
			strSql.Append("Url=@Url,");
			strSql.Append("Sequence=@Sequence,");
			strSql.Append("LastUpdateUserName=@LastUpdateUserName,");
			strSql.Append("LastUpdateUserFullName=@LastUpdateUserFullName,");
			strSql.Append("LastUpdateTime=@LastUpdateTime,");
			strSql.Append("CreateUserName=@CreateUserName,");
			strSql.Append("CreateUserFullName=@CreateUserFullName,");
			strSql.Append("CreateDateTime=@CreateDateTime");
			strSql.Append(" where PrivilegeCode=@PrivilegeCode ");
			SqlParameter[] parameters = {
					new SqlParameter("@PrivilegeName", SqlDbType.NVarChar,100),
					new SqlParameter("@PrivilegeType", SqlDbType.TinyInt,1),
					new SqlParameter("@ParentCode", SqlDbType.VarChar,50),
					new SqlParameter("@Url", SqlDbType.VarChar,2000),
					new SqlParameter("@Sequence", SqlDbType.Int,4),
					new SqlParameter("@LastUpdateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateTime", SqlDbType.DateTime),
					new SqlParameter("@CreateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateDateTime", SqlDbType.DateTime),
					new SqlParameter("@PrivilegeCode", SqlDbType.VarChar,40)};
			parameters[0].Value = model.PrivilegeName;
			parameters[1].Value = model.PrivilegeType;
			parameters[2].Value = model.ParentCode;
			parameters[3].Value = model.Url;
			parameters[4].Value = model.Sequence;
			parameters[5].Value = model.LastUpdateUserName;
			parameters[6].Value = model.LastUpdateUserFullName;
			parameters[7].Value = model.LastUpdateTime;
			parameters[8].Value = model.CreateUserName;
			parameters[9].Value = model.CreateUserFullName;
			parameters[10].Value = model.CreateDateTime;
			parameters[11].Value = model.PrivilegeCode;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string PrivilegeCode)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SYS_Privilege ");
			strSql.Append(" where PrivilegeCode=@PrivilegeCode ");
			SqlParameter[] parameters = {
					new SqlParameter("@PrivilegeCode", SqlDbType.VarChar,40)			};
			parameters[0].Value = PrivilegeCode;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string PrivilegeCodelist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SYS_Privilege ");
			strSql.Append(" where PrivilegeCode in ("+PrivilegeCodelist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.SYS_Privilege GetModel(string PrivilegeCode)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 PrivilegeCode,PrivilegeName,PrivilegeType,ParentCode,Url,Sequence,LastUpdateUserName,LastUpdateUserFullName,LastUpdateTime,CreateUserName,CreateUserFullName,CreateDateTime from SYS_Privilege ");
			strSql.Append(" where PrivilegeCode=@PrivilegeCode ");
			SqlParameter[] parameters = {
					new SqlParameter("@PrivilegeCode", SqlDbType.VarChar,40)			};
			parameters[0].Value = PrivilegeCode;

			RBS.Core.Model.SYS_Privilege model=new RBS.Core.Model.SYS_Privilege();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.SYS_Privilege DataRowToModel(DataRow row)
		{
			RBS.Core.Model.SYS_Privilege model=new RBS.Core.Model.SYS_Privilege();
			if (row != null)
			{
				if(row["PrivilegeCode"]!=null)
				{
					model.PrivilegeCode=row["PrivilegeCode"].ToString();
				}
				if(row["PrivilegeName"]!=null)
				{
					model.PrivilegeName=row["PrivilegeName"].ToString();
				}
				if(row["PrivilegeType"]!=null && row["PrivilegeType"].ToString()!="")
				{
					model.PrivilegeType=int.Parse(row["PrivilegeType"].ToString());
				}
				if(row["ParentCode"]!=null)
				{
					model.ParentCode=row["ParentCode"].ToString();
				}
				if(row["Url"]!=null)
				{
					model.Url=row["Url"].ToString();
				}
				if(row["Sequence"]!=null && row["Sequence"].ToString()!="")
				{
					model.Sequence=int.Parse(row["Sequence"].ToString());
				}
				if(row["LastUpdateUserName"]!=null)
				{
					model.LastUpdateUserName=row["LastUpdateUserName"].ToString();
				}
				if(row["LastUpdateUserFullName"]!=null)
				{
					model.LastUpdateUserFullName=row["LastUpdateUserFullName"].ToString();
				}
				if(row["LastUpdateTime"]!=null && row["LastUpdateTime"].ToString()!="")
				{
					model.LastUpdateTime=DateTime.Parse(row["LastUpdateTime"].ToString());
				}
				if(row["CreateUserName"]!=null)
				{
					model.CreateUserName=row["CreateUserName"].ToString();
				}
				if(row["CreateUserFullName"]!=null)
				{
					model.CreateUserFullName=row["CreateUserFullName"].ToString();
				}
				if(row["CreateDateTime"]!=null && row["CreateDateTime"].ToString()!="")
				{
					model.CreateDateTime=DateTime.Parse(row["CreateDateTime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select PrivilegeCode,PrivilegeName,PrivilegeType,ParentCode,Url,Sequence,LastUpdateUserName,LastUpdateUserFullName,LastUpdateTime,CreateUserName,CreateUserFullName,CreateDateTime ");
			strSql.Append(" FROM SYS_Privilege ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" PrivilegeCode,PrivilegeName,PrivilegeType,ParentCode,Url,Sequence,LastUpdateUserName,LastUpdateUserFullName,LastUpdateTime,CreateUserName,CreateUserFullName,CreateDateTime ");
			strSql.Append(" FROM SYS_Privilege ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM SYS_Privilege ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.PrivilegeCode desc");
			}
			strSql.Append(")AS Row, T.*  from SYS_Privilege T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "SYS_Privilege";
			parameters[1].Value = "PrivilegeCode";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

